﻿namespace NTLib.Core.Exceptions
{
    using System;
    using System.Reflection;
    /// <summary>
    /// Raised when the attribute mandatory is missing.
    /// </summary>
    public class AttributeMandatoryException : Exception
    {
        #region CTOR

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeMandatoryException" /> class.
        /// </summary>
        /// <param name="attributeType">Type of the attribute missing.</param>
        /// <param name="origin">The origin.</param>
        /// <param name="message">The message.</param>
        /// <param name="innerException">The inner exception.</param>
        public AttributeMandatoryException(Type attributeType, Type origin, string message = null, Exception innerException = null)
            : base(message, innerException)
        {
            this.MissingAttributeType = attributeType;
            this.OriginType = origin;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeMandatoryException" /> class.
        /// </summary>
        /// <param name="attributeType">Type of the attribute missing.</param>
        /// <param name="origin">The origin.</param>
        /// <param name="message">The message.</param>
        /// <param name="innerException">The inner exception.</param>
        public AttributeMandatoryException(Type attributeType, TypeInfo origin, string message = null, Exception innerException = null)
            : base(message, innerException)
        {
            this.MissingAttributeType = attributeType;
            this.OriginType = origin.AsType();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the type of the missing attribute.
        /// </summary>
        /// <value>
        /// The type of the missing attribute.
        /// </value>
        public Type MissingAttributeType { get; }

        /// <summary>
        /// Gets the type of the origin.
        /// </summary>
        /// <value>
        /// The type of the origin.
        /// </value>
        public Type OriginType { get; }


        #endregion

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        public override string Message
        {
            get
            {
                return string.Format("The attribute '{0}' is missing on type '{2}'.\n{1}", MissingAttributeType, base.Message, OriginType);
            }
        }

    }
}
